<?php 

include dirname(__FILE__).'/../../../DBConnector/idiorm/config2.php';

class store_product_cat_links extends Load_use
{
	private $list = '';
	
	function getBlock()
	{
		GLOBAL $view;
		$url = $_SERVER['REQUEST_URI'];
		$links = explode( '/', substr( $url, 1 ) );  //Разбиваю урл по страницам
		$lCount = count( $links );
	
		if( $this->view->brand_id && $this->view->templ != 'brand' )
		{
			$this->conn->query( '
				SELECT parents, COUNT( page_id ) AS count_product 
				FROM store_products_cache 
				WHERE `brand_id` = '.$this->view->brand_id.' AND `price_retail` > 0 AND `pp` IS NOT NULL 
				GROUP BY parents');
			while( $row = $this->conn->fetch() )
				$count_products[$row->parents] = $row->count_product;
				
			$this->conn->query( 'SELECT p.id, p.templ, p.parents, p.url, p.title, a.params FROM pages AS p 
									LEFT JOIN pages_attachments AS a ON p.id = a.page_id 
										WHERE p.templ IN( "category", "sub_category" )
											ORDER BY p.zin');
			while( $row = $this->conn->fetch() )
				$navi[$row->templ][( $row->parents ? $row->parents : 'n' )][] = $row;

			foreach( $navi['category']['n'] as $main_cat )
			{
				if( isset( $navi['sub_category']['#'.$main_cat->id.'#'] ) )
				{
					foreach( $navi['sub_category']['#'.$main_cat->id.'#'] as $cat )
					{
						$count_product = isset( $count_products[$cat->parents.'#'.$cat->id.'#'] ) ? $count_products[$cat->parents.'#'.$cat->id.'#'] : 0;
						if( isset( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] ) )
						{
							foreach( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] as $sub_cat )
							{
								$count = isset( $count_products[$cat->parents.'#'.$cat->id.'##'.$sub_cat->id.'#'] ) ? $count_products[$cat->parents.'#'.$cat->id.'##'.$sub_cat->id.'#'] : 0;
								$count_product += $count;
							}
							$count_products[$cat->parents.'#'.$cat->id.'#'] = $count_product;
						}
					}
				}
			}
		}
		else
		{
			global $count_products, $navi;
		}
		
		
		if( $this->view->templ == 'brand' )
		{
			$this->conn->query('
				SELECT title, url, pa.params,
					( SELECT COUNT(`page_id`) FROM `store_products_cache` WHERE `brand_id` = '.$this->view->brand_id.' AND `category_id` = `p`.`id` AND `price_retail` > 0 AND `pp` IS NOT NULL  ) AS `count_product` 
					FROM `pages` AS `p`
						LEFT JOIN `pages_attachments` AS `pa` ON `pa`.`page_id`  = `p`.`id`
							WHERE `p`.`templ` = "category"');

			while( $cat = $this->conn->fetch() )
				if( $cat->count_product )
					$this->getList( $cat );
		}
		elseif( isset( $navi['sub_category'][$this->view->parents.'#'.$this->view->id.'#'] ) )
		{
			foreach( $navi['sub_category'][$this->view->parents.'#'.$this->view->id.'#'] as $cat )
			{
				$cat->count_product = isset( $count_products[$cat->parents.'#'.$cat->id.'#'] ) ? $count_products[$cat->parents.'#'.$cat->id.'#'] : 0;
				if( $cat->count_product ) 
					$this->getList( $cat );
			}
		}

		
		if( $lCount == 1 && $view->brand != '' && str_replace('/', '', $url) == getGoodUrl($view->brand)) {
			$h1 = '<h1 class="big-title">Бытовая техника '.( $this->view->brand ? ' '.$this->view->brand : '' ).'</h1>';
		}
		else
			$h1 = '<h1 class="big-title">'.$this->view->title.( $this->view->brand ? ' '.$this->view->brand : '' ).'</h1>';
			
                
                if($this->view->brand) {
                    
                    $query_1 = ORM::for_table('brand_description')->where('id', $this->view->brand_id)->find_one();
                    $brand_text = ($query_1) ? $query_1->text : '';
                    print_r($this->view->brand_id);
                    return $h1.
				'<div class="entry">' . $brand_text . '</div> 
				<div class="directory">
                                					<ul>'.$this->list.'</ul>

				</div>';
                } else {
                    return $h1.
				'<div class="entry"> '.$this->view->text1.'</div> 
				<div class="directory">
					<ul>'.$this->list.'</ul>
				</div>';
                }
			
	}
	
	
	
	function getList( $cat )
	{
		GLOBAL $view;
		$url = $_SERVER['REQUEST_URI'];
		$links = explode( '/', substr( $url, 1 ) );  //Разбиваю урл по страницам
		$lCount = count( $links );
		$img = unserialize( $cat->params );
		
		

		if( $lCount == 1 && $view->brand != '' && str_replace('/', '', $url) == getGoodUrl($view->brand)) {
			if( isset( $img[0] ) && file_exists( HOME_PATH.'/inc/images/banners/category'.$img[0] ) ) {
				$path_to_pic = '/inc/images/banners/category'.$img[0];
			} else {
				$path_to_pic = '/inc/images/bg/no_image.jpg';
			}
			
			$cat_h2_titles = $cat->title.' '.$view->brand;
		} else {
			if( isset( $img[0] ) && file_exists( HOME_PATH.'/inc/images/categories'.$img[0] ) ) {
				$path_to_pic = '/inc/images/categories'.$img[0];
			} else {
				$path_to_pic = '/inc/images/bg/no_image.jpg';
			}
			
			$cat_h2_titles = $cat->title;
		}

		$img = $this->load->module('imager','getThumbName',array( 'original_name' => $path_to_pic, 'size'=> 'a' ) );
		
		$url_before = !$this->view->brand_url ? ( $this->view->url_before ? $this->view->url_before.'/' : '' ).$this->view->url : '';
		$url = ( $url_before ? $url_before.'/' : '' ).$cat->url.( $this->view->brand_url ? '/'.$this->view->brand_url : '' ); 

		$this->list .= '
			<li>
				<div class="global-photo">
					<a href="'.$url.'"><img src="'.$img.'" alt="'.$cat->title.' '.$view->brand.'"/></a>
				</div>
				<h2><a href="'.$url.'">'.$cat_h2_titles.'</a></h2>
				<dl class="directory__number">
					
					<dd>'.$cat->count_product.'</dd>
				</dl>
			</li>';
	}
}